"""
数据管理页面
提供数据源管理、数据质量监控、数据导入导出等功能
"""

import streamlit as st
import pandas as pd
from datetime import datetime, timedelta
import asyncio
from typing import Dict, List, Any, Optional

from ..services.data_service import DataService
from ..components.common import (
    create_sidebar_navigation, make_api_request,
    display_loading_spinner, show_success_message, show_error_message
)
from ..components.metrics import create_metric_card, create_status_indicator
from ..components.charts import create_line_chart, create_bar_chart, create_heatmap
from ..components.tables import create_data_table, create_editable_table
from ..components.filters import create_date_filter, create_multiselect_filter


@st.cache_data(ttl=300)
def get_data_service():
    """获取数据服务实例"""
    return DataService()


@st.cache_data(ttl=60)
def get_data_sources_overview():
    """获取数据源概览数据"""
    try:
        data_service = get_data_service()
        result = data_service.get_data_management_data("sources_overview")
        if result and result.get("success"):
            return result.get("data", {})
    except Exception as e:
        st.error(f"获取数据源概览失败: {str(e)}")
    
    # 降级数据
    return {
        "active_sources": {"value": "12", "delta": "2"},
        "total_data_size": {"value": "2.5TB", "delta": "150GB"},
        "daily_updates": {"value": "8", "delta": "3"},
        "connection_status": {"value": "正常", "delta": "100%"}
    }


@st.cache_data(ttl=60)
def get_data_sources_list():
    """获取数据源列表"""
    try:
        data_service = get_data_service()
        result = data_service.get_data_management_data("sources_list")
        if result and result.get("success"):
            return pd.DataFrame(result.get("data", []))
    except Exception as e:
        st.error(f"获取数据源列表失败: {str(e)}")
    
    # 降级数据
    return pd.DataFrame({
        '数据源名称': ['Wind数据', '同花顺数据', 'Yahoo Finance', '东方财富', 'Tushare', 'Alpha Vantage'],
        '类型': ['股票', '股票', '股票', '股票', '股票', '股票'],
        '状态': ['正常', '正常', '正常', '异常', '正常', '正常'],
        '最后更新': ['2024-01-15 09:30:00', '2024-01-15 09:25:00', '2024-01-15 09:20:00',
                   '2024-01-14 15:30:00', '2024-01-15 09:15:00', '2024-01-15 09:10:00'],
        '数据量': ['500MB', '300MB', '200MB', '150MB', '400MB', '100MB'],
        '更新频率': ['实时', '实时', '15分钟', '日', '实时', '小时']
    })


@st.cache_data(ttl=60)
def get_data_quality_overview():
    """获取数据质量概览"""
    try:
        data_service = get_data_service()
        result = data_service.get_data_management_data("quality_overview")
        if result and result.get("success"):
            return result.get("data", {})
    except Exception as e:
        st.error(f"获取数据质量概览失败: {str(e)}")
    
    # 降级数据
    return {
        "completeness": {"value": "95.2%", "delta": "2.1%"},
        "accuracy": {"value": "98.7%", "delta": "0.5%"},
        "timeliness": {"value": "92.8%", "delta": "-1.2%"},
        "anomaly_count": {"value": "127", "delta": "23"}
    }


@st.cache_data(ttl=300)
def get_data_quality_trends():
    """获取数据质量趋势数据"""
    try:
        data_service = get_data_service()
        result = data_service.get_data_management_data("quality_trends")
        if result and result.get("success"):
            return pd.DataFrame(result.get("data", []))
    except Exception as e:
        st.error(f"获取数据质量趋势失败: {str(e)}")
    
    # 降级数据
    dates = pd.date_range(start='2024-01-01', end='2024-01-15', freq='D')
    return pd.DataFrame({
        '日期': dates,
        '完整性': [95 + i * 0.1 + (i % 3) * 0.5 for i in range(len(dates))],
        '准确性': [98 + i * 0.05 + (i % 2) * 0.3 for i in range(len(dates))],
        '及时性': [92 + i * 0.08 + (i % 4) * 0.4 for i in range(len(dates))]
    })


@st.cache_data(ttl=300)
def get_source_quality_distribution():
    """获取数据源质量分布"""
    try:
        data_service = get_data_service()
        result = data_service.get_data_management_data("source_quality")
        if result and result.get("success"):
            return pd.DataFrame(result.get("data", []))
    except Exception as e:
        st.error(f"获取数据源质量分布失败: {str(e)}")
    
    # 降级数据
    return pd.DataFrame({
        '数据源': ['Wind数据', '同花顺数据', 'Yahoo Finance', '东方财富', 'Tushare'],
        '完整性': [96.5, 94.2, 92.8, 89.3, 97.1],
        '准确性': [98.9, 97.5, 96.8, 95.2, 99.1],
        '及时性': [94.2, 91.7, 88.5, 85.9, 95.8]
    })


@st.cache_data(ttl=60)
def get_anomaly_data():
    """获取异常数据详情"""
    try:
        data_service = get_data_service()
        result = data_service.get_data_management_data("anomaly_data")
        if result and result.get("success"):
            return pd.DataFrame(result.get("data", []))
    except Exception as e:
        st.error(f"获取异常数据失败: {str(e)}")
    
    # 降级数据
    return pd.DataFrame({
        '时间': ['2024-01-15 09:30:00', '2024-01-15 09:25:00', '2024-01-15 09:20:00'],
        '数据源': ['Wind数据', '同花顺数据', 'Yahoo Finance'],
        '异常类型': ['数据缺失', '数值异常', '格式错误'],
        '影响字段': ['收盘价', '成交量', '开盘价'],
        '严重程度': ['中', '高', '低'],
        '状态': ['已处理', '处理中', '待处理']
    })


@st.cache_data(ttl=300)
def get_export_preview_data(filters=None):
    """获取导出预览数据"""
    try:
        data_service = get_data_service()
        params = {"filters": filters} if filters else {}
        result = data_service.get_data_management_data("export_preview", params)
        if result and result.get("success"):
            return pd.DataFrame(result.get("data", []))
    except Exception as e:
        st.error(f"获取导出预览失败: {str(e)}")
    
    # 降级数据
    return pd.DataFrame({
        '股票代码': ['000001', '000002', '600000'],
        '股票名称': ['平安银行', '万科A', '浦发银行'],
        '收盘价': [12.50, 18.30, 8.90],
        '成交量': [1000000, 800000, 1200000]
    })


@st.cache_data(ttl=30)
def get_monitoring_overview():
    """获取数据监控概览"""
    try:
        data_service = get_data_service()
        result = data_service.get_data_management_data("monitoring_overview")
        if result and result.get("success"):
            return result.get("data", {})
    except Exception as e:
        st.error(f"获取监控概览失败: {str(e)}")
    
    # 降级数据
    return {
        "realtime_data_volume": {"value": "15.2K", "delta": "1.2K"},
        "data_latency": {"value": "2.3s", "delta": "0.5s"},
        "error_rate": {"value": "0.05%", "delta": "0.02%"},
        "system_load": {"value": "65%", "delta": "5%"}
    }


def render_data_management_page():
    """渲染数据管理页面"""
    st.title("📊 数据管理")
    
    # 侧边栏导航
    create_sidebar_navigation()
    
    # 创建标签页
    tab1, tab2, tab3, tab4, tab5 = st.tabs([
        "数据源管理", "数据质量", "数据导入", "数据导出", "数据监控"
    ])
    
    with tab1:
        render_data_sources_tab()
    
    with tab2:
        render_data_quality_tab()
    
    with tab3:
        render_data_import_tab()
    
    with tab4:
        render_data_export_tab()
    
    with tab5:
        render_data_monitoring_tab()


def render_data_sources_tab():
    """渲染数据源管理标签页"""
    st.header("数据源管理")
    
    # 获取数据源概览数据
    with st.spinner("加载数据源概览..."):
        overview_data = get_data_sources_overview()
    
    # 数据源概览
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        create_metric_card(
            "活跃数据源",
            overview_data.get("active_sources", {}).get("value", "12"),
            "📡",
            delta=overview_data.get("active_sources", {}).get("delta", "2"),
            delta_color="normal"
        )
    
    with col2:
        create_metric_card(
            "总数据量",
            overview_data.get("total_data_size", {}).get("value", "2.5TB"),
            "💾",
            delta=overview_data.get("total_data_size", {}).get("delta", "150GB"),
            delta_color="normal"
        )
    
    with col3:
        create_metric_card(
            "今日更新",
            overview_data.get("daily_updates", {}).get("value", "8"),
            "🔄",
            delta=overview_data.get("daily_updates", {}).get("delta", "3"),
            delta_color="normal"
        )
    
    with col4:
        create_metric_card(
            "连接状态",
            overview_data.get("connection_status", {}).get("value", "正常"),
            "✅",
            delta=overview_data.get("connection_status", {}).get("delta", "100%"),
            delta_color="normal"
        )
    
    st.divider()
    
    # 数据源列表
    st.subheader("数据源列表")
    
    # 获取数据源列表数据
    with st.spinner("加载数据源列表..."):
        data_sources = get_data_sources_list()
    
    # 状态过滤器
    status_filter = create_multiselect_filter(
        data_sources, '状态', '筛选状态', key='data_source_status'
    )
    
    if status_filter:
        filtered_data = data_sources[data_sources['状态'].isin(status_filter)]
    else:
        filtered_data = data_sources
    
    # 显示数据源表格
    create_data_table(filtered_data, "数据源详情")
    
    # 数据源操作
    st.subheader("数据源操作")
    
    col1, col2, col3 = st.columns(3)
    
    with col1:
        if st.button("🔄 刷新所有数据源", use_container_width=True):
            with st.spinner("正在刷新数据源..."):
                try:
                    data_service = get_data_service()
                    result = data_service.refresh_data_sources()
                    if result and result.get("success"):
                        show_success_message("数据源刷新成功")
                        # 清除缓存以获取最新数据
                        st.cache_data.clear()
                        st.rerun()
                    else:
                        show_error_message(f"数据源刷新失败: {result.get('message', '未知错误')}")
                except Exception as e:
                    show_error_message(f"数据源刷新失败: {str(e)}")
    
    with col2:
        if st.button("➕ 添加数据源", use_container_width=True):
            st.session_state.show_add_data_source = True
    
    with col3:
        if st.button("⚙️ 数据源配置", use_container_width=True):
            st.session_state.show_data_source_config = True
    
    # 添加数据源对话框
    if st.session_state.get('show_add_data_source', False):
        render_add_data_source_dialog()
    
    # 数据源配置对话框
    if st.session_state.get('show_data_source_config', False):
        render_data_source_config_dialog()


def render_add_data_source_dialog():
    """渲染添加数据源对话框"""
    with st.expander("添加新数据源", expanded=True):
        col1, col2 = st.columns(2)
        
        with col1:
            source_name = st.text_input("数据源名称")
            source_type = st.selectbox("数据源类型", ["股票", "期货", "基金", "债券", "外汇"])
            api_url = st.text_input("API地址")
        
        with col2:
            api_key = st.text_input("API密钥", type="password")
            update_frequency = st.selectbox("更新频率", ["实时", "分钟", "小时", "日"])
            description = st.text_area("描述")
        
        col1, col2, col3 = st.columns(3)
        
        with col1:
            if st.button("测试连接"):
                with st.spinner("测试连接中..."):
                    # 模拟连接测试
                    st.success("连接测试成功")
        
        with col2:
            if st.button("保存"):
                if source_name and api_url:
                    try:
                        data_service = get_data_service()
                        result = data_service.add_data_source({
                            "name": source_name,
                            "type": source_type,
                            "api_url": api_url,
                            "api_key": api_key,
                            "update_frequency": update_frequency,
                            "description": description
                        })
                        if result and result.get("success"):
                            show_success_message(f"数据源 '{source_name}' 添加成功")
                            st.session_state.show_add_data_source = False
                            # 清除缓存以获取最新数据
                            st.cache_data.clear()
                            st.rerun()
                        else:
                            show_error_message(f"添加数据源失败: {result.get('message', '未知错误')}")
                    except Exception as e:
                        show_error_message(f"添加数据源失败: {str(e)}")
                else:
                    show_error_message("请填写必要信息")
        
        with col3:
            if st.button("取消"):
                st.session_state.show_add_data_source = False
                st.rerun()


def render_data_source_config_dialog():
    """渲染数据源配置对话框"""
    with st.expander("数据源配置", expanded=True):
        st.subheader("全局配置")
        
        col1, col2 = st.columns(2)
        
        with col1:
            max_retry = st.number_input("最大重试次数", min_value=1, max_value=10, value=3)
            timeout = st.number_input("超时时间(秒)", min_value=5, max_value=300, value=30)
        
        with col2:
            cache_duration = st.number_input("缓存时长(分钟)", min_value=1, max_value=1440, value=60)
            parallel_requests = st.number_input("并发请求数", min_value=1, max_value=20, value=5)
        
        st.subheader("数据质量配置")
        
        col1, col2 = st.columns(2)
        
        with col1:
            enable_validation = st.checkbox("启用数据验证", value=True)
            auto_clean = st.checkbox("自动数据清洗", value=True)
        
        with col2:
            alert_threshold = st.slider("异常数据告警阈值(%)", 0, 100, 10)
            backup_enabled = st.checkbox("启用数据备份", value=True)
        
        col1, col2 = st.columns(2)
        
        with col1:
            if st.button("保存配置"):
                show_success_message("配置保存成功")
                st.session_state.show_data_source_config = False
                st.rerun()
        
        with col2:
            if st.button("取消"):
                st.session_state.show_data_source_config = False
                st.rerun()


def render_data_quality_tab():
    """渲染数据质量标签页"""
    st.header("数据质量监控")
    
    # 获取数据质量概览数据
    with st.spinner("加载数据质量概览..."):
        quality_overview = get_data_quality_overview()
    
    # 数据质量概览
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        create_metric_card(
            "数据完整性",
            quality_overview.get("completeness", {}).get("value", "95.2%"),
            "✅",
            delta=quality_overview.get("completeness", {}).get("delta", "2.1%"),
            delta_color="normal"
        )
    
    with col2:
        create_metric_card(
            "数据准确性",
            quality_overview.get("accuracy", {}).get("value", "98.7%"),
            "🎯",
            delta=quality_overview.get("accuracy", {}).get("delta", "0.5%"),
            delta_color="normal"
        )
    
    with col3:
        create_metric_card(
            "数据及时性",
            quality_overview.get("timeliness", {}).get("value", "92.8%"),
            "⏰",
            delta=quality_overview.get("timeliness", {}).get("delta", "-1.2%"),
            delta_color="inverse"
        )
    
    with col4:
        create_metric_card(
            "异常数据",
            quality_overview.get("anomaly_count", {}).get("value", "127"),
            "⚠️",
            delta=quality_overview.get("anomaly_count", {}).get("delta", "23"),
            delta_color="inverse"
        )
    
    st.divider()
    
    # 数据质量趋势图
    st.subheader("数据质量趋势")
    
    # 获取数据质量趋势数据
    with st.spinner("加载数据质量趋势..."):
        quality_data = get_data_quality_trends()
    
    create_line_chart(
        quality_data, 
        x_col='日期', 
        y_cols=['完整性', '准确性', '及时性'],
        title="数据质量趋势",
        height=400
    )
    
    # 数据源质量分布
    st.subheader("各数据源质量分布")
    
    # 获取数据源质量分布数据
    with st.spinner("加载数据源质量分布..."):
        source_quality = get_data_source_quality()
    
    create_bar_chart(
        source_quality,
        x_col='数据源',
        y_cols=['完整性', '准确性', '及时性'],
        title="数据源质量对比"
    )
    
    # 异常数据详情
    st.subheader("异常数据详情")
    
    # 获取异常数据详情
    with st.spinner("加载异常数据详情..."):
        anomaly_data = get_anomaly_data()
    
    create_data_table(anomaly_data, "异常数据记录")


def render_data_import_tab():
    """渲染数据导入标签页"""
    st.header("数据导入")
    
    # 导入方式选择
    import_method = st.radio(
        "选择导入方式",
        ["文件上传", "API导入", "数据库导入", "实时数据流"],
        horizontal=True
    )
    
    if import_method == "文件上传":
        render_file_upload_section()
    elif import_method == "API导入":
        render_api_import_section()
    elif import_method == "数据库导入":
        render_database_import_section()
    else:
        render_stream_import_section()


def render_file_upload_section():
    """渲染文件上传部分"""
    st.subheader("文件上传")
    
    # 文件上传
    uploaded_file = st.file_uploader(
        "选择文件",
        type=['csv', 'xlsx', 'json', 'parquet'],
        help="支持CSV、Excel、JSON、Parquet格式"
    )
    
    if uploaded_file is not None:
        # 显示文件信息
        st.info(f"文件名: {uploaded_file.name}")
        st.info(f"文件大小: {uploaded_file.size / 1024:.2f} KB")
        
        # 预览数据
        try:
            # 获取数据导入预览
            with st.spinner("处理文件数据..."):
                import_preview = get_data_import_preview({
                    'file_name': uploaded_file.name,
                    'file_size': uploaded_file.size,
                    'file_type': uploaded_file.name.split('.')[-1]
                })
            
            if import_preview and 'data' in import_preview:
                df = pd.DataFrame(import_preview['data'])
                st.subheader("数据预览")
                st.dataframe(df.head(10))
            else:
                # 降级处理：直接读取文件
                if uploaded_file.name.endswith('.csv'):
                    df = pd.read_csv(uploaded_file)
                elif uploaded_file.name.endswith(('.xlsx', '.xls')):
                    df = pd.read_excel(uploaded_file)
                elif uploaded_file.name.endswith('.json'):
                    df = pd.read_json(uploaded_file)
                elif uploaded_file.name.endswith('.parquet'):
                    df = pd.read_parquet(uploaded_file)
                
                st.subheader("数据预览")
                st.dataframe(df.head(10))
            
            # 数据映射配置
            st.subheader("字段映射")
            
            col1, col2 = st.columns(2)
            
            with col1:
                st.write("**源字段**")
                source_columns = df.columns.tolist()
                for col in source_columns[:5]:  # 显示前5个字段
                    st.text(col)
            
            with col2:
                st.write("**目标字段**")
                target_fields = ['股票代码', '股票名称', '开盘价', '收盘价', '最高价', '最低价', '成交量']
                for i, col in enumerate(source_columns[:5]):
                    st.selectbox(
                        f"映射 {col}",
                        options=['不映射'] + target_fields,
                        key=f"mapping_{i}"
                    )
            
            # 导入选项
            st.subheader("导入选项")
            
            col1, col2, col3 = st.columns(3)
            
            with col1:
                skip_duplicates = st.checkbox("跳过重复数据", value=True)
                validate_data = st.checkbox("验证数据格式", value=True)
            
            with col2:
                batch_size = st.number_input("批次大小", min_value=100, max_value=10000, value=1000)
                table_name = st.text_input("目标表名", value="imported_data")
            
            with col3:
                if st.button("开始导入", type="primary"):
                    try:
                        data_service = get_data_service()
                        import_result = data_service.import_data({
                            'file_name': uploaded_file.name,
                            'table_name': table_name,
                            'batch_size': batch_size,
                            'skip_duplicates': skip_duplicates,
                            'validate_data': validate_data,
                            'data_count': len(df) if 'df' in locals() else 0
                        })
                        
                        if import_result and import_result.get("success"):
                            show_success_message(f"成功导入 {import_result.get('imported_count', 0)} 条数据")
                        else:
                            show_error_message("数据导入失败")
                    except Exception as e:
                        show_error_message(f"导入过程中出现错误: {str(e)}")
        
        except Exception as e:
            show_error_message(f"文件解析失败: {str(e)}")


def render_api_import_section():
    """渲染API导入部分"""
    st.subheader("API数据导入")
    
    col1, col2 = st.columns(2)
    
    with col1:
        api_url = st.text_input("API地址")
        api_key = st.text_input("API密钥", type="password")
        request_method = st.selectbox("请求方法", ["GET", "POST"])
    
    with col2:
        data_format = st.selectbox("数据格式", ["JSON", "XML", "CSV"])
        update_mode = st.selectbox("更新模式", ["增量", "全量"])
        schedule_enabled = st.checkbox("启用定时导入")
    
    if schedule_enabled:
        col1, col2 = st.columns(2)
        with col1:
            schedule_frequency = st.selectbox("导入频率", ["每小时", "每日", "每周"])
        with col2:
            schedule_time = st.time_input("导入时间")
    
    # API参数配置
    st.subheader("API参数")
    
    if 'api_params' not in st.session_state:
        st.session_state.api_params = [{'key': '', 'value': ''}]
    
    for i, param in enumerate(st.session_state.api_params):
        col1, col2, col3 = st.columns([2, 2, 1])
        
        with col1:
            param['key'] = st.text_input(f"参数名 {i+1}", value=param['key'], key=f"param_key_{i}")
        
        with col2:
            param['value'] = st.text_input(f"参数值 {i+1}", value=param['value'], key=f"param_value_{i}")
        
        with col3:
            if st.button("删除", key=f"delete_param_{i}"):
                st.session_state.api_params.pop(i)
                st.rerun()
    
    if st.button("添加参数"):
        st.session_state.api_params.append({'key': '', 'value': ''})
        st.rerun()
    
    # 测试和导入
    col1, col2 = st.columns(2)
    
    with col1:
        if st.button("测试API"):
            if api_url:
                try:
                    data_service = get_data_service()
                    test_result = data_service.test_api_connection({
                        'api_url': api_url,
                        'api_key': api_key,
                        'request_method': request_method,
                        'data_format': data_format,
                        'params': {param['key']: param['value'] for param in st.session_state.api_params if param['key']}
                    })
                    
                    if test_result and test_result.get("success"):
                        st.success("API连接测试成功")
                        st.json(test_result.get("sample_data", {"status": "success", "records": 1000}))
                    else:
                        show_error_message("API连接测试失败")
                except Exception as e:
                    show_error_message(f"API测试过程中出现错误: {str(e)}")
            else:
                show_error_message("请输入API地址")
    
    with col2:
        if st.button("开始导入"):
            if api_url:
                try:
                    data_service = get_data_service()
                    import_result = data_service.import_api_data({
                        'api_url': api_url,
                        'api_key': api_key,
                        'request_method': request_method,
                        'data_format': data_format,
                        'update_mode': update_mode,
                        'params': {param['key']: param['value'] for param in st.session_state.api_params if param['key']}
                    })
                    
                    if import_result and import_result.get("success"):
                        show_success_message(f"API数据导入成功，共导入 {import_result.get('imported_count', 0)} 条数据")
                    else:
                        show_error_message("API数据导入失败")
                except Exception as e:
                    show_error_message(f"API导入过程中出现错误: {str(e)}")
            else:
                show_error_message("请输入API地址")


def render_database_import_section():
    """渲染数据库导入部分"""
    st.subheader("数据库导入")
    
    # 数据库连接配置
    col1, col2 = st.columns(2)
    
    with col1:
        db_type = st.selectbox("数据库类型", ["MySQL", "PostgreSQL", "SQLite", "Oracle", "SQL Server"])
        host = st.text_input("主机地址", value="localhost")
        port = st.number_input("端口", min_value=1, max_value=65535, value=3306)
    
    with col2:
        database = st.text_input("数据库名")
        username = st.text_input("用户名")
        password = st.text_input("密码", type="password")
    
    # SQL查询
    st.subheader("SQL查询")
    
    sql_query = st.text_area(
        "SQL语句",
        value="SELECT * FROM your_table WHERE date >= '2024-01-01'",
        height=100
    )
    
    # 导入选项
    col1, col2, col3 = st.columns(3)
    
    with col1:
        chunk_size = st.number_input("分块大小", min_value=1000, max_value=100000, value=10000)
    
    with col2:
        target_table = st.text_input("目标表名", value="imported_data")
    
    with col3:
        if_exists = st.selectbox("如果表存在", ["replace", "append", "fail"])
    
    # 测试和导入
    col1, col2 = st.columns(2)
    
    with col1:
        if st.button("测试连接"):
            if all([host, database, username]):
                with st.spinner("测试数据库连接..."):
                    # 模拟数据库连接测试
                    st.success("数据库连接测试成功")
            else:
                show_error_message("请填写完整的连接信息")
    
    with col2:
        if st.button("开始导入"):
            if all([host, database, username, sql_query]):
                with st.spinner("正在导入数据..."):
                    # 模拟数据库导入
                    progress_bar = st.progress(0)
                    for i in range(100):
                        progress_bar.progress(i + 1)
                    
                    show_success_message("数据库数据导入成功")
            else:
                show_error_message("请填写完整的信息")


def render_stream_import_section():
    """渲染实时数据流导入部分"""
    st.subheader("实时数据流")
    
    # 数据流配置
    col1, col2 = st.columns(2)
    
    with col1:
        stream_type = st.selectbox("数据流类型", ["Kafka", "Redis Stream", "WebSocket", "MQTT"])
        stream_url = st.text_input("连接地址")
        topic = st.text_input("主题/频道")
    
    with col2:
        consumer_group = st.text_input("消费者组")
        batch_size = st.number_input("批处理大小", min_value=1, max_value=10000, value=100)
        buffer_size = st.number_input("缓冲区大小", min_value=1000, max_value=1000000, value=10000)
    
    # 数据处理配置
    st.subheader("数据处理")
    
    col1, col2 = st.columns(2)
    
    with col1:
        enable_filter = st.checkbox("启用数据过滤")
        if enable_filter:
            filter_condition = st.text_area("过滤条件", value="price > 0")
    
    with col2:
        enable_transform = st.checkbox("启用数据转换")
        if enable_transform:
            transform_rules = st.text_area("转换规则", value="price = price * 100")
    
    # 流状态监控
    st.subheader("流状态监控")
    
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        create_status_indicator("连接状态", "已连接", "success")
    
    with col2:
        create_metric_card("消息速率", "1.2K/s", "📈")
    
    with col3:
        create_metric_card("处理延迟", "50ms", "⏱️")
    
    with col4:
        create_metric_card("错误率", "0.1%", "⚠️")
    
    # 控制按钮
    col1, col2, col3 = st.columns(3)
    
    with col1:
        if st.button("启动数据流"):
            show_success_message("数据流已启动")
    
    with col2:
        if st.button("暂停数据流"):
            show_success_message("数据流已暂停")
    
    with col3:
        if st.button("停止数据流"):
            show_success_message("数据流已停止")


def render_data_export_tab():
    """渲染数据导出标签页"""
    st.header("数据导出")
    
    # 导出配置
    col1, col2 = st.columns(2)
    
    with col1:
        export_format = st.selectbox("导出格式", ["CSV", "Excel", "JSON", "Parquet", "SQL"])
        date_range = create_date_filter("数据日期范围")
    
    with col2:
        data_source = st.multiselect("选择数据源", ["股票数据", "基金数据", "期货数据", "债券数据"])
        compression = st.selectbox("压缩格式", ["无", "ZIP", "GZIP"])
    
    # 字段选择
    st.subheader("字段选择")
    
    available_fields = [
        "股票代码", "股票名称", "开盘价", "收盘价", "最高价", "最低价", 
        "成交量", "成交额", "涨跌幅", "换手率"
    ]
    
    selected_fields = st.multiselect(
        "选择要导出的字段",
        available_fields,
        default=available_fields
    )
    
    # 过滤条件
    st.subheader("过滤条件")
    
    col1, col2 = st.columns(2)
    
    with col1:
        price_range = st.slider("价格范围", 0.0, 1000.0, (0.0, 1000.0))
        volume_min = st.number_input("最小成交量", min_value=0, value=0)
    
    with col2:
        market_cap_range = st.slider("市值范围(亿)", 0, 10000, (0, 10000))
        exclude_st = st.checkbox("排除ST股票", value=True)
    
    # 导出预览
    st.subheader("导出预览")
    
    # 获取导出预览数据
    with st.spinner("生成导出预览..."):
        export_filters = {
            'data_source': data_source,
            'export_format': export_format,
            'date_range': date_range,
            'selected_fields': selected_fields,
            'price_range': price_range,
            'volume_min': volume_min,
            'market_cap_range': market_cap_range,
            'exclude_st': exclude_st
        }
        preview_data = get_export_preview_data(export_filters)
    
    st.dataframe(preview_data)
    st.info(f"预计导出 {len(preview_data)} 条记录")
    
    # 导出按钮
    col1, col2, col3 = st.columns(3)
    
    with col1:
        if st.button("预览导出", use_container_width=True):
            show_success_message("导出预览生成成功")
    
    with col2:
        if st.button("立即导出", use_container_width=True):
            with st.spinner("正在导出数据..."):
                try:
                    data_service = get_data_service()
                    export_result = data_service.export_data({
                        'export_format': export_format,
                        'data_source': data_source,
                        'date_range': date_range,
                        'selected_fields': selected_fields,
                        'filters': {
                            'price_range': price_range,
                            'volume_min': volume_min,
                            'market_cap_range': market_cap_range,
                            'exclude_st': exclude_st
                        },
                        'compression': compression
                    })
                    
                    if export_result and export_result.get("success"):
                        show_success_message(f"数据导出成功，文件路径: {export_result.get('file_path', '未知')}")
                    else:
                        show_error_message(f"数据导出失败: {export_result.get('message', '未知错误')}")
                except Exception as e:
                    show_error_message(f"导出过程中出现错误: {str(e)}")
    
    with col3:
        if st.button("定时导出", use_container_width=True):
            st.session_state.show_schedule_export = True


def render_data_monitoring_tab():
    """渲染数据监控标签页"""
    st.header("数据监控")
    
    # 获取监控概览数据
    with st.spinner("加载监控概览..."):
        monitoring_overview = get_monitoring_overview()
    
    # 实时监控指标
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        create_metric_card(
            "实时数据量",
            monitoring_overview.get("realtime_data_volume", {}).get("value", "15.2K"),
            "📊",
            delta=monitoring_overview.get("realtime_data_volume", {}).get("delta", "1.2K"),
            delta_color="normal"
        )
    
    with col2:
        create_metric_card(
            "数据延迟",
            monitoring_overview.get("data_latency", {}).get("value", "2.3s"),
            "⏱️",
            delta=monitoring_overview.get("data_latency", {}).get("delta", "0.5s"),
            delta_color="inverse"
        )
    
    with col3:
        create_metric_card(
            "错误率",
            monitoring_overview.get("error_rate", {}).get("value", "0.05%"),
            "⚠️",
            delta=monitoring_overview.get("error_rate", {}).get("delta", "0.02%"),
            delta_color="inverse"
        )
    
    with col4:
        create_metric_card(
            "系统负载",
            monitoring_overview.get("system_load", {}).get("value", "65%"),
            "💻",
            delta=monitoring_overview.get("system_load", {}).get("delta", "5%"),
            delta_color="normal"
        )
    
    st.divider()
    
    # 数据流量监控
    st.subheader("数据流量监控")
    
    # 模拟实时数据流量
    import time
    import random
    
    if 'monitoring_data' not in st.session_state:
        st.session_state.monitoring_data = []
    
    # 自动刷新数据
    if st.button("🔄 刷新监控数据"):
        try:
            data_service = get_data_service()
            monitoring_data = data_service.get_realtime_monitoring_data()
            
            if monitoring_data and monitoring_data.get("success"):
                new_data = monitoring_data.get("data", {})
                st.session_state.monitoring_data.append({
                    '时间': datetime.now(),
                    '数据量': new_data.get('data_volume', random.randint(10000, 20000)),
                    '延迟': new_data.get('latency', random.uniform(1.0, 5.0)),
                    '错误数': new_data.get('error_count', random.randint(0, 10))
                })
            else:
                # 降级数据
                current_time = datetime.now()
                new_data = {
                    '时间': current_time,
                    '数据量': random.randint(10000, 20000),
                    '延迟': random.uniform(1.0, 5.0),
                    '错误数': random.randint(0, 10)
                }
                st.session_state.monitoring_data.append(new_data)
            
            # 保持最近50个数据点
            if len(st.session_state.monitoring_data) > 50:
                st.session_state.monitoring_data = st.session_state.monitoring_data[-50:]
                
            show_success_message("监控数据刷新成功")
        except Exception as e:
            show_error_message(f"刷新监控数据失败: {str(e)}")
    
    # 显示监控图表
    if st.session_state.monitoring_data:
        monitoring_df = pd.DataFrame(st.session_state.monitoring_data)
        
        create_line_chart(
            monitoring_df,
            x_col='时间',
            y_col='数据量',
            title="实时数据流量",
            height=300
        )
        
        col1, col2 = st.columns(2)
        
        with col1:
            create_line_chart(
                monitoring_df,
                x_col='时间',
                y_col='延迟',
                title="数据延迟监控",
                height=250
            )
        
        with col2:
            create_line_chart(
                monitoring_df,
                x_col='时间',
                y_col='错误数',
                title="错误数监控",
                height=250
            )
    
    # 告警配置
    st.subheader("告警配置")
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.write("**数据量告警**")
        data_volume_threshold = st.number_input("数据量阈值", min_value=1000, value=5000)
        enable_volume_alert = st.checkbox("启用数据量告警")
        
        st.write("**延迟告警**")
        latency_threshold = st.number_input("延迟阈值(秒)", min_value=1.0, value=10.0)
        enable_latency_alert = st.checkbox("启用延迟告警")
    
    with col2:
        st.write("**错误率告警**")
        error_rate_threshold = st.slider("错误率阈值(%)", 0.0, 10.0, 1.0)
        enable_error_alert = st.checkbox("启用错误率告警")
        
        st.write("**通知方式**")
        notification_methods = st.multiselect(
            "选择通知方式",
            ["邮件", "短信", "微信", "钉钉"],
            default=["邮件"]
        )
    
    if st.button("保存告警配置"):
        show_success_message("告警配置保存成功")


if __name__ == "__main__":
    render_data_management_page()